import {
		mapGetters,
		mapActions
	} from 'vuex';
import { getCompanyLists,getBannerLists,orderCounts,getUserCompanys,getUserInfos,tencentImAuths,getMailLists } from '@/common/api.js';
const indexMixin = {
	data() {
		return {
			showAdd: true,
			arrdata: [],//公司列表
			fromdata:{
				page:1,
				limit:999,
				company_id:0
			},
			selobj:{
				logo_img: "",
				title: "",
				id:0,
				statu: false
			},
			list:[],// banner图片
			objdata2:{
				haveOrder: 0,
				waitOrder: 0,
				myOrder: 0,
			},
			bannerList:[], // banner图片
			hostGoodsList: [],// 热门商品
			goodsList: [], // 商品列表
			selectIndex: 0, //选中的商品分类
			live_room_id: '',
			typeList: [{
				category_name: '全部',
				category_id: 0,
				count : 1,
				page : 1,
				loadingType : 'more',
				list : []
			}],
			
			scrollTop: 0,
			noData: false,
			load_more1: '', //下拉加载文字
			load_sta: false, //是否是加载状态
			currentSubIndex:0,//选中品牌序号
			message:[{
				msg:'123重要信息传达不到位？试试用公告试试用公告试试用公告试试用公告',
				id:1
			},{
				msg:'11重要信息传达不到位？试试用公告试试用公告试试用公告试试用公告',
				id:2
			}],
			datalist: [],
			dyid: '',
			forcomid: 0,
			comments: '',
			showtip: false,
			is_input: false, //评论输入框
			isVoice: false,
			recording:false,
			showtip: false, //是否显示弹窗1
			content1: '顶部弹 popup',
			type1: '', //弹窗显示方式
		}
	},
	watch: {
		companyInfo(val,old) {
			this.getcompanyInfo(val,old)
		}
	},
	onShow() {
		if(this.hasLogin) {
			//设置购物车数量状态
			//this.getCount();
		}
		this.getData();
	},
	computed: {
		...mapGetters(['companyInfo','selIndex','userSig','userInfo','companyRole','userRole']),
		proOrderStatu(){
			return this.companyRole.pro_role
		},
		popOrderStatu(){
			return this.companyRole.pop_role
		},
		propOrderStatu(){
			return this.companyRole.prop_role
		},
		dynamicStatu(){
			return this.userRole.dynamicStatu
		}
	},
	onReachBottom() {
		//this.getGoodsData()
	},
	methods: {
		...mapActions('user',['setCompanyInfo','setSelIndex','setUserInfo','setUserSig','setContactList','setCompanyRole','setUserRole']),
		
		//用户IM登录
		getusersig() {
			console.log('IM登录')
		},
		async getData2() { // 切换公司-初始化-数据
		    uni.showLoading({
		    	mask: true
		    })
			// 获取公司信息
			let resdata1 = await getUserCompanys(); 
			let currentSubIndex = 0
			if(!!this.selIndex){
				currentSubIndex = this.selIndex;
			}else{
				this.setSelIndex(currentSubIndex); //设置缓存
			}
			this.arrdata = resdata1.data;
			this.selobj = resdata1.data[currentSubIndex];
			//获取用户信息
			let resdata2 = await getUserInfos({company_id: this.selobj.id}); 
			//设置缓存 公司-用户-通讯录
			this.setCompanyInfo(this.selobj)
			this.setUserInfo(resdata2.data);
			
			// 获取首页轮播
			await this.getBanner();
			// 统计进行中-待审批-我的下单 数量
			await this.getWorkCount();
			//获取动态评论
			await this.isRefresh();
			
			// 获取公司通讯录
			let params1 = { company_id:this.selobj.id,type:1}
			let params2 = { company_id:this.selobj.id,type:2}
			let resdata3 = await getMailLists(params1);
			let resdata4 = await getMailLists(params2);
			this.contact = [...resdata3.data, ...resdata4.data]
			this.setContactList(this.contact);
			uni.hideLoading()
			uni.stopPullDownRefresh();
		},
		async getData(){ //初始化-数据
			let resdata1 = await getUserCompanys(); // 获取公司信息
			let currentSubIndex = 0
			if(!!this.selIndex){
				currentSubIndex = this.selIndex;
			}else{
				this.setSelIndex(currentSubIndex);
			}
			this.arrdata = resdata1.data;
			this.selobj = resdata1.data[currentSubIndex];
			
			// 获取首页轮播
			await this.getBanner();
			// 统计进行中-待审批-我的下单 数量
			await this.getWorkCount();
			uni.stopPullDownRefresh();
		},
		getBanner(){
			this.fromdata.company_id = this.selobj.id;
			getBannerLists(this.fromdata).then(res=>{
				if(res.code === 2000){
					this.list = res.data.data;
					this.$forceUpdate()
				}
			})
		},
		getWorkCount(){
			orderCounts({company_id:this.companyInfo.id}).then(res=>{
				if(res.code === 2000){
					this.objdata2 = res.data;
				}
			})
		},
		getcompanyInfo(val,old){
			//获取公司权限
			this.getCompanyRole();
		},
		getCompanyRole(){
			console.log('获取公司权限')
			let companyRole = {
				bas_role:true,
				team_role:true,
				news_role:true,
				pop_role:false,
				pro_role:false,
				prop_role:false,
			}
			let userRole = {
				proStatu:false, //产品属性设置-1
				popStatu:false, //POP编码设置-2
				propStatu:false, //道具编码设置-3
				textureStatu:false, //POP材质设置-4
				quotationStatu:false, //供应商报价单-5
				
				memberStatu:false, //成员管理-6
				groupStatu:false, //组别管理-7
				powerStatu:false, //权限管理-8
				
				newsStatu:false, //新闻管理-9
				noticeStatu:false, //公告&通知管理-10
				dynamicStatu:false, //动态管理-11
				
				proOrderStatu:false, //POP画册管理-12
				
				popOrderStatu:false , //POP下单管理-13
				countStatu1:false , //数据统计功能-14
				
				propOrderStatu:false , //POP下单管理-15
				countStatu2:false , //数据统计功能-16
			}
			let role_arr = []
			if(!!this.userInfo){
				if(this.userInfo.role_arr && this.userInfo.role_arr.length>0){
					role_arr = this.userInfo.role_arr
				}
			}
			if(!!this.companyInfo){
				if(this.companyInfo.roleArr && this.companyInfo.roleArr.length>0){
					this.companyInfo.roleArr.forEach(item=>{
						if(item === '1'){
							companyRole.bas_role = true;
							if(this.userInfo.type === 0){
								userRole.proStatu = true;
								userRole.popStatu = true;
								userRole.powerStatu = true;
								userRole.textureStatu = true;
								userRole.quotationStatu = true;
							}else if(this.userInfo.type === 1){
								role_arr.forEach(ite=>{
									if(parseInt(ite) === 1){
										userRole.proStatu = true;
									}else if(parseInt(ite) === 2){
										userRole.popStatu = true;
									}else if(parseInt(ite) === 3){
										userRole.powerStatu = true;
									}else if(parseInt(ite) === 4){
										userRole.textureStatu = true;
									}else if(parseInt(ite) === 5){
										userRole.quotationStatu = true;
									}
								})
							}
						}else if(item === '2'){
							companyRole.team_role = true;
							if(this.userInfo.type === 0){
								userRole.memberStatu = true;
								userRole.groupStatu = true;
								userRole.propStatu = true;
							}else if(this.userInfo.type === 1){
								role_arr.forEach(ite=>{
									if(parseInt(ite) === 6){
										userRole.memberStatu = true;
									}else if(parseInt(ite) === 7){
										userRole.groupStatu = true;
									}else if(parseInt(ite) === 8){
										userRole.propStatu = true;
									}
								})
							}
						}else if(item === '3'){
							companyRole.news_role = true;
							if(this.userInfo.type === 0){
								userRole.newsStatu = true;
								userRole.noticeStatu = true;
								userRole.dynamicStatu = true;
							}else if(this.userInfo.type === 1){
								role_arr.forEach(ite=>{
									if(parseInt(ite) === 9){
										userRole.newsStatu = true;
									}else if(parseInt(ite) === 10){
										userRole.noticeStatu = true;
									}else if(parseInt(ite) === 11){
										userRole.dynamicStatu = true;
									}
								})
							}
						}else if(item === '4'){
							companyRole.pro_role = true;
							if(this.userInfo.type === 0){
								userRole.proOrderStatu = true;
							}else if(this.userInfo.type === 1){
								role_arr.forEach(ite=>{
									if(parseInt(ite) === 12){
										userRole.proOrderStatu = true;
									}
								})
							}
						}else if(item === '5'){
							companyRole.pop_role = true;
							if(this.userInfo.type === 0){
								userRole.popOrderStatu = true;
								userRole.countStatu1 = true;
							}else if(this.userInfo.type === 1){
								role_arr.forEach(ite=>{
									if(parseInt(ite) === 13){
										userRole.popOrderStatu = true;
									}else if(parseInt(ite) === 14 ){
										userRole.countStatu1 = true;
									}
								})
							}
						}else if(item === '6'){
							companyRole.prop_role = true;
							if(this.userInfo.type === 0){
								userRole.propOrderStatu = true;
								userRole.countStatu2 = true;
							}else if(this.userInfo.type === 1){
								role_arr.forEach(ite=>{
									if(parseInt(ite) === 15){
										userRole.propOrderStatu = true;
									}else if(parseInt(ite) === 16){
										userRole.countStatu2 = true;
									}
								})
							}
						}
					})
				}
			}
			this.setCompanyRole(companyRole)
			this.setUserRole(userRole)
		},
	}
}
export default indexMixin
